﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AddressableAssets;
using UnityEngine.EventSystems;
using UnityEngine.ResourceManagement.AsyncOperations;
using UnityEngine.SceneManagement;

public enum WayPointType
{
    Battle,
    Event,
    Recover,
    Boss
}

public class WayPoint : MonoBehaviour
{
    public bool isCanSetMapByOnClick = false;
    public WayPointType wayPointType;

    public GameObject[] nextWayPoints;

    public bool isSelected = false;
    private static readonly int IsActive = Animator.StringToHash("isActive");

    public AssetReference lineAssetRefer;
    
    public void Click()
    {
        if (!isCanSetMapByOnClick)
        {
            Debug.Log("当前关卡不可用");
            return;
        }

        if (isSelected)
        {
            isSelected = false;
            isCanSetMapByOnClick = false;

            Debug.Log($"准备跳转至 {wayPointType.ToString()} 场景");
            GameObject.FindGameObjectWithTag(StaticDataManager.GAME_SESSION_TAG).GetComponent<GameSession>().SetCurrentWayPoint(this);
            var sl = GameObject.FindGameObjectWithTag(StaticDataManager.SCENE_LOADER_TAG).GetComponent<SceneLoader>();
            sl.isActivateOnComplete = true;
            sl.LoadNextSceneAsync("3_Battle", LoadSceneMode.Single);
        }

        Debug.Log($"当前已选择关卡 {gameObject.ToString()}");
        
        // TODO 遍历其他的 把他们的isSelected都关掉

        
        isSelected = true;
        var focusSelect = GameObject.FindGameObjectWithTag(StaticDataManager.FOCUS_SELECT_TAG);
        focusSelect.transform.localScale = new Vector3(50f, 50f, 50f);
        focusSelect.transform.position = this.gameObject.transform.position;
    }
    
    private void Start()
    {
        if (nextWayPoints.Length > 0)
        {
            foreach (var nextWayPoint in nextWayPoints)
            {
                DrawLine(nextWayPoint.transform.position);
            }
        }
        
        SetAnimationState();
    }

    private void EnableWayPoint()
    {
        isCanSetMapByOnClick = true;
        SetAnimationState();
    }

    private void DisableWayPoint()
    {
        isCanSetMapByOnClick = false;
        SetAnimationState();
    }

    private void SetAnimationState()
    {
        GetComponent<Animator>().SetBool(IsActive, isCanSetMapByOnClick);
    }

    private void DrawLine(Vector3 targetPosition)
    {
//        GameObject line = Instantiate(Resources.Load<GameObject>("prefabs/LinePrefab"), this.transform.parent, true);
        lineAssetRefer.InstantiateAsync(this.transform.parent).Completed += handle =>
        {
            DrawLineA(handle.Result, targetPosition);
        };
    }
    
    private void DrawLineA(GameObject line, Vector3 targetPosition)
    {
        // TODO 计算到目标位置的长度
        var distance = Vector2.Distance(transform.position, targetPosition) * .9f;

        // TODO 修改line的sprite的width
        line.GetComponent<SpriteRenderer>().size = new Vector2(distance, .125f);
        // TODO 然后到距离一半的地方放下这个line
        var newPos = (transform.position + targetPosition) / 2;
        // 然后面向目标位置
        newPos.z = 0;
        line.transform.position = newPos;
        var dir = targetPosition - transform.position;
        var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
        line.transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
    }
    
    public void EnableNextScene()
    {
        foreach (var wayPoint in GameObject.FindObjectsOfType<WayPoint>())
        {
            wayPoint.DisableWayPoint();
        }
        
        foreach (GameObject wayPoint in nextWayPoints)
        {
            wayPoint.GetComponent<WayPoint>().EnableWayPoint();
        }
    }

}
